﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public struct Score
    {
        public int h, w, e1,e2;
        public Score(int h, int w, int e1,int e2)
        { this.h = h; this.w = w; this.e1 = e1;this.e2 = e2; }
    }

    public partial class Form1 : Form
    {        
        //第一步 创建基本的画图环境
        private Graphics g;
        private Bitmap buff;
        private Thread t1;//游戏线程
        public static bool isstart = false;                        //游戏是否开始 是否启动线程
private static int n = 10;
        //第二步 初始化游戏属性
        /// <summary>
        /// 黑棋 1 白棋 2
        /// </summary>
        public int[,] chessBox = new int[n, n];
        //设置当前选手
        private int current = 1;
        public Form1()
        {
            InitializeComponent();
            this.Width = 600;
            this.Height = 600;            
            //设置显示图源控件的几个属性，防闪烁
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
            buff = new Bitmap(500, 500);
            g = Graphics.FromImage(buff);
            t1 = new Thread(Game);
        }

        Point Check(int x1,int y1)
        {
            return new Point(x1/50,y1/50);
        }
        void Game()
        {
            while (isstart)
            {
            }
        }

        bool successful(Point cur)
        {
            int num = 0;
            //水平判断
            for (int i = 1; i <= 4 && cur.Y + i < n; i++)
            {
                if (chessBox[cur.X, cur.Y + i] == current)
                    num++;
                else
                    break;
            }
            for (int i = 1; i <= 4 && cur.Y - i >= 0; i++)
            {
                if (chessBox[cur.X, cur.Y - i] == current)
                    num++;
                else
                    break;
            }
            if (num >= 4) return true;

            num = 0;
            //判断
            for (int i = 1; i <= 4 && cur.X + i < n; i++)
            {
                if (chessBox[cur.X + i, cur.Y] == current) num++;
                else break;
            }
            for (int i = 1; i <= 4 && cur.X - i >= 0; i++)
            {
                if (chessBox[cur.X - i, cur.Y] == current) num++;
                else break;
            }
            if (num >= 4) return true;

            num = 0;
            //水平判断
            for (int i = 1; i <= 4 && cur.X + i < n && cur.Y + i < n; i++)
            {
                if (chessBox[cur.X + i, cur.Y + i] == current) num++;
                else break;
            }
            for (int i = 1; i <= 4 && cur.X - i >= 0 && cur.Y - i >= 0; i++)
            {
                if (chessBox[cur.X - i, cur.Y - i] == current) num++;
                else break;
            }
            if (num >= 4) return true;

            num = 0;
            //水平判断
            for (int i = 1; i <= 4 && cur.X - i >= 0 && cur.Y + i < n; i++)
            {
                if (chessBox[cur.X - i, cur.Y + i] == current) num++;
                else break;
            }
            for (int i = 1; i <= 4 && cur.X + i < n && cur.Y - i >= 0; i++)
            {
                if (chessBox[cur.X + i, cur.Y - i] == current) num++;
                else break;
            }
            if (num >= 4) return true;
            return false;
        }
        Score getScore(Point cur)
        {
            int h=0, w=0, e1=0,e2=0;
            //水平判断
            for (int i = 1; i <= 4 && cur.Y + i < n; i++)
            {
                if (chessBox[cur.X, cur.Y + i] == current)
                    h++;
                else
                    break;
            }
            for (int i = 1; i <= 4 && cur.Y - i >= 0; i++)
            {
                if (chessBox[cur.X, cur.Y - i] == current)
                    h++;
                else
                    break;
            }
            
            //判断
            for (int i = 1; i <= 4 && cur.X + i < n; i++)
            {
                if (chessBox[cur.X + i, cur.Y] == current) w++;
                else break;
            }
            for (int i = 1; i <= 4 && cur.X - i >= 0; i++)
            {
                if (chessBox[cur.X - i, cur.Y] == current) w++;
                else break;
            }

            for (int i = 1; i <= 4 && cur.X + i < n && cur.Y + i < n; i++)
            {
                if (chessBox[cur.X + i, cur.Y + i] == current) e1++;
                else break;
            }
            for (int i = 1; i <= 4 && cur.X - i >= 0 && cur.Y - i >= 0; i++)
            {
                if (chessBox[cur.X - i, cur.Y - i] == current) e1++;
                else break;
            }

            for (int i = 1; i <= 4 && cur.X - i >= 0 && cur.Y + i < n; i++)
            {
                if (chessBox[cur.X - i, cur.Y + i] == current) e2++;
                else break;
            }
            for (int i = 1; i <= 4 && cur.X + i < n && cur.Y - i >= 0; i++)
            {
                if (chessBox[cur.X + i, cur.Y - i] == current) e2++;
                else break;
            }
            return new Score(h,w,e1,e2);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            isstart = true;
            t1.Start();
        }

        private void MouseUP(object sender, MouseEventArgs e)
        {
            Point xy = Check(e.X, e.Y);
            if (xy.X < 0 || xy.Y < 0 || xy.X >= n || xy.Y >= n) return;
            if (chessBox[xy.X, xy.Y] == 0)
            {
                chessBox[xy.X, xy.Y] = current;
                //强制重绘
                this.Invalidate();
                if (successful(xy))
                {
                    MessageBox.Show(string.Format("游戏结束，{0}获胜了", current));
                    current = 2;
                    for (int i = 0; i < n; i++)
                        for (int j = 0; j < n; j++)
                            chessBox[i, j] = 0;
                    this.Invalidate();
                }
                current = (3 - current);
                textBox1.Text = "";
                for (int j = 0; j < n; j++)
                {
                    for (int i = 0; i < n; i++)
                    {
                        Score s = getScore(new Point(i,j));
                        if (chessBox[i, j] == 0) textBox1.Text += ((s.h+s.w+s.e1+s.e2)/4).ToString();
                        if (chessBox[i, j] != 0) textBox1.Text += "+";
                    }
                    textBox1.Text += "\n";
                }
            }
        }

        private void GameOver(object sender, FormClosingEventArgs e)
        {
            isstart = false;
            //等待进程结束
            t1.Join();
            buff.Dispose();
            g.Dispose();
        }

        private void draw(object sender, PaintEventArgs e)
        {
            g.Clear(Color.White);
            for (int i = 0; i <= n; i++)
                g.DrawLine(new Pen(new SolidBrush(Color.Black), 3), 25 + i * 50, 25, 25 + i * 50, 475);
            for (int i = 0; i <= n; i++)
                g.DrawLine(new Pen(new SolidBrush(Color.Black), 3), 25, 25 + i * 50, 475, 25 + i * 50);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    if (chessBox[i, j] == 1) g.FillEllipse(new SolidBrush(Color.Black), i * 50 + 5, j * 50 + 5, 40, 40);
                    if (chessBox[i, j] == 2)
                    {
                        g.DrawEllipse(new Pen(new SolidBrush(Color.Black), 3), i * 50 + 5, j * 50 + 5, 40, 40);
                        g.FillEllipse(new SolidBrush(Color.White ), i * 50 + 6, j * 50 + 6, 38, 38);
                    }
                }
     
            e.Graphics.DrawImage(buff, 0, 0);
        }
    }
}
